package com.bountytask.controller;

import com.alibaba.fastjson.JSONObject;
import com.bountytask.entity.Message;
import com.bountytask.service.MessageService;
import com.bountytask.service.impl.MessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;


    /**
     * 添加留言信息
     */
    @PostMapping("/message/add")
    public JSONObject addMessage(@RequestBody JSONObject recData){
        JSONObject resultJson = new JSONObject(true);
        return messageService.addOne(recData.getLong("userid"),recData.getString("mes"));
    }
    /**
     * 根据留言id查信息
     */
    @GetMapping("/message/get/{id}")
    public JSONObject getOne(@PathVariable long id){
        JSONObject resultJson = new JSONObject(true);
        Message message = messageService.getOne(id);
        if (message==null){
            resultJson.put("code",404);
            resultJson.put("message","此信息不存在");

            return resultJson;
        }
        resultJson.put("code",200);
        resultJson.put("message","成功");
        resultJson.put("data",message);
        return resultJson;
    }

    /**
     * 查询全部留言信息
     * @return
     */
    @GetMapping("/message/getall")
    public JSONObject getAll()
    {
        List<Message> list=messageService.getAll();
        JSONObject jsonObject=new JSONObject(true);
        jsonObject.put("code",200);
        jsonObject.put("message","成功");
        jsonObject.put("data",list);
        return jsonObject;
    }
}
